import { compType } from '../NeedChange/UIConfig';
import UIContainer from './UIContainer';
const { ccclass, property } = cc._decorator;

let refreshLabel = (oldValue: any, newValue: any, comp: cc.Component) => {
    (comp as cc.Label).string = newValue;
}
let refreshProgressBar = (oldValue: any, newValue: any, comp: cc.ProgressBar) => {
    (comp as cc.ProgressBar).progress = newValue;
}


export const compCallBack = {
    Label: refreshLabel,
    ProgressBar: refreshProgressBar,
}

export default class UIUtils {
    //查找需要关注的节点
    //参数1：传入每个UI页面的根节点
    //参数2：传入每个页面的存储容器
    static findNode(node: cc.Node, uiContainer: UIContainer) {
        //判断节点是否是需要关注的节点
        //判断方式：节点的名字是否是以“_”开始
        //先从根节点开始
        if (node.name.startsWith("_")) {
            //存储节点
            uiContainer.addNode(node.name, node);
            //存储组件
            for (let compName in compType) {
                let comp = node.getComponent(compType[compName]);
                if (!comp) {
                    continue;
                }
                uiContainer.addComp(node.name, compName, comp);
            }
        }
        //再存储这个节点中的需要关注的子节点
        for (let child of node.children) {
            this.findNode(child, uiContainer);
        }
        return;
    }


    static refreshComp(oldValue: any, newValue: any, comp: cc.Component) {
        if (!comp) {
            cc.error("组件刷新失败，组件不存在");
            return;
        }
        for (let compName in compType) {
            if (comp instanceof compType[compName]) {
                return compCallBack[compName](oldValue, newValue, comp);
            }
        }
    }
}


